package com.lai.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lai.hospital.dao.CaseDao;
import com.lai.hospital.dto.CaseDTO;
import com.lai.hospital.entity.Case;
import com.lai.hospital.enums.FilePathEnum;
import com.lai.hospital.service.CaseService;
import com.lai.hospital.strategy.context.UploadStrategyContext;
import com.lai.hospital.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : lai
 * @version V1.0
 * @Project: hospital-springboot
 * @Package com.lai.hospital.service.impl
 * @Description: 病例服务
 * @date Date : 2022年12月07日 11:26
 */
@Service
public class CaseServiceImpl extends ServiceImpl<CaseDao, Case> implements CaseService {

    @Autowired
    private CaseDao caseDao;

    @Autowired
    private UploadStrategyContext uploadStrategyContext;

    @Override
    public List<Case> listCase(Integer patientId) {
        return caseDao.selectList(new LambdaQueryWrapper<Case>().eq(Case::getPatientId, patientId));
    }

    @Override
    public List<CaseDTO> listCaseByPatientId(Integer patientId) {
        List<Case> cases = caseDao.selectList(new LambdaQueryWrapper<Case>().eq(Case::getPatientId, patientId)
                .orderByDesc(Case::getConsultationsTime));
        //格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        BeanCopyUtils.copyList(cases, CaseDTO.class);
        if (cases.size() > 0 || Objects.nonNull(cases)) {
            return BeanCopyUtils.copyList(cases, CaseDTO.class).stream()
                    .map(item -> {
                        String createTime = item.getCreateTime().format(formatter);
                        item.setUploadTime(createTime.split(" ")[0]);
                        return item;
                    }).collect(Collectors.toList());
        }
        return null;

    }

    @Override
    public Case getCaseById(Integer caseId) {
        return caseDao.selectById(caseId);
    }

    @Override
    public String uploadCase(MultipartFile file) {
        //图片上传
        return uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.CASE.getPath());
    }

    @Override
    public void saveOrUpdateCase(Case cases) {
        this.saveOrUpdate(cases);
    }
}
